//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.controller;

import com.example.common.Result;
import com.example.entity.Club;
import com.example.service.ClubService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/club"})
public class ClubController {
    @Resource
    private ClubService clubService;

    public ClubController() {
    }

    @GetMapping({"/selectAll"})
    public Result selectAll(Club club) {
        List<Club> list = this.clubService.selectAll(club);
        return Result.success(list);
    }

    @GetMapping({"/selectById/{id}"})
    public Result selectById(@PathVariable Integer id) {
        Club club = this.clubService.selectById(id);
        return Result.success(club);
    }

    @GetMapping({"/selectByOne"})
    public Result selectByOne(@RequestParam Integer id, @RequestParam(required = false) String no) {
        Club club = this.clubService.selectById(id);
        return Result.success(club);
    }

    @GetMapping({"/selectList"})
    public Result selectList(Club club) {
        List<Club> list = this.clubService.selectList(club);
        return Result.success(list);
    }

    @GetMapping({"/selectPage"})
    public Result selectPage(Club club, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Club> pageInfo = this.clubService.selectPage(club, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping({"/selectByPresidentId/{presidentId}"})
    public Result selectByPresidentId(@PathVariable Integer presidentId) {
        List<Club> list = this.clubService.selectByPresidentId(presidentId);
        return Result.success(list);
    }

    @PostMapping({"/add"})
    public Result add(@RequestBody Club club) {
        this.clubService.add(club);
        return Result.success();
    }

    @PutMapping({"/updateById"})
    public Result updateById(@RequestBody Club club) {
        this.clubService.updateById(club);
        return Result.success();
    }

    @PutMapping({"/updateImage"})
    public Result updateImage(@RequestBody Club club) {
        this.clubService.updateImageUrl(club);
        return Result.success();
    }

    @DeleteMapping({"/deleteById/{id}"})
    public Result deleteById(@PathVariable Integer id) {
        this.clubService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping({"/deleteBatch"})
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        this.clubService.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping("/join")
    public Result join(@RequestBody Map<String, Integer> params) {
        Integer clubId = params.get("clubId");
        Integer studentId = params.get("studentId");
        if (clubId == null || studentId == null) {
            return Result.error("400", "参数错误");
        }
        clubService.joinClub(clubId, studentId);
        return Result.success();
    }

    @GetMapping("/checkMember")
    public Result checkMember(@RequestParam Integer clubId, @RequestParam Integer studentId) {
        boolean isMember = clubService.checkMember(clubId, studentId);
        return Result.success(isMember);
    }

    @GetMapping("/checkApplication")
    public Result checkApplication(@RequestParam Integer clubId, @RequestParam Integer studentId) {
        boolean isPending = clubService.checkApplication(clubId, studentId);
        return Result.success(isPending);
    }

    @PostMapping("/quit")
    public Result quitClub(@RequestBody Map<String, Integer> params) {
        Integer clubId = params.get("clubId");
        Integer studentId = params.get("studentId");
        
        if (clubId == null || studentId == null) {
            return Result.error("-1", "参数错误");
        }
        
        try {
            // 删除社团成员关系
            clubService.quitClub(clubId, studentId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("-1", "退出社团失败：" + e.getMessage());
        }
    }

    @GetMapping("/selectByStudentId/{studentId}")
    public Result selectByStudentId(@PathVariable Integer studentId) {
        List<Club> list = clubService.selectByStudentId(studentId);
        return Result.success(list);
    }
}
